package services

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/dao"
	"hft/Http/Models/request"
	"hft/Http/Services/base"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/global"
	"hft/tools"
	"regexp"
)

type RoomSwiper struct{}

func (*RoomSwiper) CreateRoomSwiper(claims tools.BaseClaims, data *admin.RoomSwiper) (err error) {

	var account *admin.Account

	var louPan *admin.LouPan

	var category *admin.LouPanSwiperCategory

	var room *admin.Room

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("找不到该账号信息")
	}

	err = global.OC_DB.First(&louPan, data.LouPanId).Error

	if err != nil {
		return errors.New("找不到楼盘信息")
	}

	err = global.OC_DB.First(&room, data.RoomID).Error

	if err != nil {
		return errors.New("找不到户型信息")
	}

	err = global.OC_DB.First(&category, data.Category).Error

	if err != nil {
		return errors.New("找不到该相册分类信息")
	}

	match, err := regexp.MatchString(fmt.Sprintf("(%v)", account.RegionLink), louPan.RegionLink)

	if err != nil {
		return errors.New("未知错误500")
	}

	if !match {
		return errors.New("你无权添加该户型相册")
	}

	// data.LouPanName = louPan.LouPanName.Name
	data.RoomName = room.Name
	data.CategoryName = category.Name

	err = global.OC_DB.Create(&data).Error

	if err != nil {
		return errors.New("未知错误500")
	}

	return nil
}

func (*RoomSwiper) EditRoomSwiper(claims tools.BaseClaims, data *admin.RoomSwiper) (err error) {

	var account *admin.Account

	var loupan *admin.LouPan

	var category *admin.LouPanSwiperCategory

	var room *admin.Room

	err = global.OC_DB.First(&admin.RoomSwiper{}, data.ID).Error

	if err != nil {
		return errors.New("找不到该相册信息")
	}

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("找不到该账号信息")
	}

	err = global.OC_DB.First(&loupan, data.LouPanId).Error

	if err != nil {
		return errors.New("找不到该楼盘信息")
	}

	err = global.OC_DB.First(&room, data.RoomID).Error

	if err != nil {
		return errors.New("找不到该户型信息")
	}

	err = global.OC_DB.First(&category, data.Category).Error

	if err != nil {
		return errors.New("找不到该相册分类信息")
	}

	match, err := regexp.MatchString(fmt.Sprintf("(%v)", account.RegionLink), loupan.RegionLink)

	if err != nil {
		return errors.New("未知错误500")
	}

	if !match {
		return errors.New("你无权添加该户型相册")
	}

	// data.LouPanName = loupan.LouPanName.Name
	data.RoomName = room.Name
	data.CategoryName = category.Name

	if category.IsCover {
		err = global.OC_DB.Where("id = ?", data.ID).Omit("create_time,update_time,deleted_at,category").Save(&data).Error
	} else {
		err = global.OC_DB.Where("id = ?", data.ID).Omit("create_time,update_time,deleted_at,category,go_url").Save(&data).Error
	}

	if err != nil {
		return errors.New("未知错误")
	}

	return nil
}

func (*RoomSwiper) GetSwiperList(claims tools.BaseClaims, paginate *request.RoomSwiperSearch) (data *utils.Page, err error) {
	var account *admin.Account
	var loupan *admin.LouPan

	var swipers []*admin.RoomSwiper

	if paginate.LouPanId == 0 {
		return nil, errors.New("无效的楼盘信息")
	}

	if paginate.RoomID == 0 {
		return nil, errors.New("无效的户型信息")
	}

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return nil, errors.New("找不到该账号信息")
	}

	err = global.OC_DB.First(&loupan, paginate.LouPanId).Error

	if err != nil {
		return nil, errors.New("找不到该楼盘信息")
	}

	match, err := regexp.MatchString(fmt.Sprintf("(%v)", account.RegionLink), loupan.RegionLink)

	if err != nil {
		return nil, errors.New("未知错误500")
	}

	if !match {
		return nil, errors.New("你无权获取该户型相关相册信息")
	}

	err = global.OC_DB.Scopes(dao.RoomSwiperDao.GetSwiperList(paginate, global.OC_DB)).Find(&swipers).Offset(-1).Limit(-1).Count(&paginate.Total).Error

	if err != nil {
		return nil, errors.New("未知错误500")
	}
	paginate.Data = swipers

	return &paginate.Page, nil
}

// 删除
func (*RoomSwiper) DeleteSwiper(ids []int, claims tools.BaseClaims) error {
	var louIDS []int
	var room_swipe []*admin.RoomSwiper
	var lou *admin.LouPan
	err := global.OC_DB.Find(&room_swipe, ids).Error
	if err != nil {
		return errors.New(e.ErrorInfo)
	}
	for _, v := range room_swipe {
		louIDS = append(louIDS, v.LouPanId)
	}
	if !utils.JudgeIDSSame(louIDS) || louIDS[0] == 0 {
		return errors.New(e.ErrorInfo)
	}
	err = global.OC_DB.Select("id,region_link").First(&lou, louIDS[0]).Error
	if err != nil {
		return errors.New(e.ErrorInfo)
	}
	account, err := base.FindAccount(claims, global.OC_DB)
	if err != nil {
		return err
	}
	if ok, _ := regexp.MatchString(fmt.Sprintf("^%v", account.RegionLink), lou.RegionLink); !ok {
		return errors.New(e.NoRightToOpera)
	}
	global.OC_DB.Unscoped().Delete(&admin.RoomSwiper{}, ids)
	return nil
}

var RoomSwiperService = new(RoomSwiper)
